package com.vessel.decoration.mvp.ui.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jess.arms.di.component.AppComponent;
import com.vessel.decoration.R;
import com.vessel.decoration.app.base.MySupportActivity;
import com.vessel.decoration.app.base.MySupportAdapter;
import com.vessel.decoration.app.utils.UiHelper;
import com.vessel.decoration.di.component.DaggerSearchComponent;
import com.vessel.decoration.di.module.SearchModule;
import com.vessel.decoration.mvp.contract.SearchContract;
import com.vessel.decoration.mvp.presenter.SearchPresenter;
import com.vessel.decoration.mvp.ui.adapter.SearchPopAdapter;
import com.vessel.decoration.mvp.ui.widget.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Jimq
 * @date 2017/12/8
 */

public class SearchActivity extends MySupportActivity<SearchPresenter>
        implements SearchContract.View {
    @BindView(R.id.swipe_container)
    RefreshLayout mSwipeLayout;
    @BindView(R.id.search_list)
    ListView mSearchList;
    @BindView(R.id.keywords)
    EditText keywords;
    @BindView(R.id.type)
    TextView type;

    private SearchPopAdapter mPopAdapter;
    private List<String> mPopList = new ArrayList<>();
    private int index = 0;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerSearchComponent
                .builder()
                .appComponent(appComponent)
                .searchModule(new SearchModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_search;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPopList.add("师傅");
        mPopList.add("材料");
        if (mPopAdapter == null) {
            mPopAdapter = new SearchPopAdapter();
        }
        mSearchList.setOnItemClickListener(mPresenter);

    }

    @OnClick({R.id.left, R.id.type, R.id.search})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.left:
                killMyself();
                break;
            case R.id.type:
                View contentView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.popup_search_list, null);
                ListView mLv = (ListView) contentView.findViewById(R.id.pop_shop_lv);
                mLv.setAdapter(mPopAdapter);
                mPopAdapter.setList(mPopList);
                //自适应大小
                PopupWindow popupWindow = new PopupWindow();
                popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
                popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
                popupWindow.setContentView(contentView);
                //点击外部消失
                popupWindow.setBackgroundDrawable(new BitmapDrawable());
                popupWindow.setOutsideTouchable(true);
                int[] location = new int[2];
                view.getLocationOnScreen(location);
                //在控件上方显示
                popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0] - popupWindow.getWidth() / 2, location[1] + view.getHeight());
                mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        index = i;
                        type.setText(mPopList.get(i));
                        popupWindow.dismiss();
                    }
                });
                break;
            case R.id.search:
                if (TextUtils.isEmpty(keywords.getText().toString().trim())) {
                    UiHelper.showToast(getApplicationContext(), "请输入搜索内容");
                    return;
                }
                mPresenter.getSearchResult(true, keywords.getText().toString().trim(), index);
                break;
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(String message) {
        UiHelper.showToast(this, message);
    }

    @Override
    public void launchActivity(Intent intent) {

    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void updateLoading(boolean show) {
        if (show) {
            mSwipeLayout.setRefreshing(false);
        } else {
            mSwipeLayout.setLoading(false);
        }
    }

    @Override
    public void setAdapter(MySupportAdapter adapter) {
        mSearchList.setAdapter(adapter);
    }
}
